package com.itlong.cloud.smartcard.service.impl;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.PO.TimeTaskDevicePassRecordPO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.smartcard.dao.ISmartCardEquipRecordDao;
import com.itlong.cloud.smartcard.service.ISmartCardEquipRecordService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* <desc>
*     一卡通通行记录处理类..
* </desc>
*
* @createDate 2018-01-23
*/
@Service
public class SmartCardEquipRecordServiceImpl implements ISmartCardEquipRecordService {


    private static Logger LOG = LoggerFactory.getLogger(SmartCardEquipRecordServiceImpl.class);

    @Autowired
    private ISmartCardEquipRecordDao iSmartCardEquipRecordDao;


    /**
     * <desc>
     * 添加一卡通同步到云端的通行记录（目前仅支持添加访客二维码记录）。
     * <desc/>
     *
     * @param recordPOS 通行记录信息
     * @return
     * @author LouisLewis
     * @createDate 2018-01-23
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer addEquipRecordInfos(List<TimeTaskDevicePassRecordPO> recordPOS) throws Exception {

        List<Map<String, Object>> recordsList = new ArrayList<>(); //定义需保存的房屋列表
        for (TimeTaskDevicePassRecordPO recordPO:recordPOS){
            recordPO.setSource(4);
            if ((StringUtils.isBlank(recordPO.getDeviceUnique()))
                || (null == recordPO.getPassTime())){
                LOG.info(String.format("一卡通上传给云端的通行记录不合法,放弃处理,%s",JSON.toJSONString(recordPO)));
                continue;
            }
            recordsList.add( SqlUtil.durableData(recordPO, PlatformConstants.TABLE_SAVE));
        }

        int intRes = -1;
        if (!recordsList.isEmpty()){
            intRes = iSmartCardEquipRecordDao.batchSavePassRecords(recordsList);
        }
        return intRes;

    }
}
